package com.wisdomstar.web.controller.wisdomstar;

import com.wisdomstar.common.core.domain.AjaxResult;
import com.wisdomstar.common.enums.BucketTypeEnum;
import com.wisdomstar.common.exception.base.BaseException;
import com.wisdomstar.common.utils.StringUtils;
import com.wisdomstar.common.utils.file.MultipartFileUtil;
import com.wisdomstar.common.utils.minio.MinioClientUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author RinMi
 * @since 2023/6/4
 */

@RestController
@RequestMapping("/ws/file")
@Tag(name = "ws-file-controller", description = "文件管理接口")
public class WsFileController {

    @Resource
    private MinioClientUtil minioClientUtil;

    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @PostMapping("uploadFile")
    public AjaxResult uploadFile(MultipartFile file) {
        try {
            //获取内容类型
            String contentType = file.getContentType();
            //获取文件类型
            String fileType = BucketTypeEnum.getFileType(contentType);
            //判断文件类型是否存在
            if (StringUtils.isEmpty(fileType)) {
                return AjaxResult.error("文件类型错误");
            }
            //存储，获取文件名称
            String filename = minioClientUtil.uploadFile(fileType, file.getOriginalFilename(),
                    file.getInputStream(), file.getSize(), file.getContentType());
            return AjaxResult.success("上传成功", fileType + ":" + filename);
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("上传失败");
        }
    }

    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @PostMapping("downloadFile")
    public void downloadFile(@ApiParam("文件路径") @RequestParam String path, HttpServletResponse response) {
        try {
            String[] strings = path.split(":");
            //获取文件类型
            String fileType = BucketTypeEnum.checkFileType(strings[0])? strings[0]: null;
            if (fileType == null){
                System.out.println("路径错误");
            }
            //获取文件名称
            String filename = strings[1];
            //获取内容类型
            String contentType = BucketTypeEnum.getContentType(fileType);
            //获取文件
            InputStream inputStream = minioClientUtil.downloadFile(fileType, filename);
            int count = 0;
            for (;inputStream.available() == 0 && count++ <= 5;) {}
            MultipartFileUtil.createFile(response, inputStream, contentType, filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
